const express = require("express");
const pool = require("../pool.js");
const c = express.Router();

// 首页 推荐 粥谱查询
c.get("/recommend/:cid", (req, res) => {
  // 获取cid的值
  let obj = req.params;
  let num = Number(obj.cid);
  let num2 = (num - 1) * 6;
  //console.log(num2);
  // 查找数据库的数据  每页6个内容
  pool.query("select * from menu limit ?,6 ", [num2], (err, result) => {
    if (err) throw err;
    // console.log(result);
    res.send({
      code: 200,
      msg: result,
    });
  });
});
// 粥谱查询详情
c.get("/recipe", (req, res) => {
  // 获取得到传入的值
  let mu_id = req.query.mu_id;
  // sql语句查找数据库的数据
  let sql =
    "select * from ((select * from step left outer join menu on step.suid=mu_id) union (select * from step right outer join menu on step.suid=mu_id)) as B where B.sintroduce IS NOT NULL and B.mu_id=?";
  pool.query(sql, [mu_id], (err, result) => {
    if (err) throw err;
    // console.log(result);
    res.send({
      code: 200,
      msg: result,
    });
  });
});

c.get("/recipe/food", (req, res) => {
  // 获取得到传入的值
  let mu_id = req.query.mu_id;
  // sql语句查找数据库的数据
  let sql =
    "select * from ((select * from foods left outer join menu on foods.fuid=mu_id) union (select * from foods right outer join menu on foods.fuid=mu_id)) as A where A.food IS NOT NULL and A.mu_id=?";
  pool.query(sql, [mu_id], (err, result) => {
    if (err) throw err;
    // console.log(result);
    res.send({
      code: 201,
      msg: result,
    });
  });
});

// 粥铺创建
c.post("/create", (req, res) => {
  // let obj = req.body;
  let body = req.body;

  //SQL 语句
  // mu_introduce 介绍   mu_tips 小贴士, mu_avatar 用户头像 mu_aname 用户名称
  let sql =
    "INSERT  menu(mu_title,mu_pic,mu_introduce,mu_tips) VALUES(?,?,?,?)";

  // let sql2="INSERT "
  pool.query(sql, [title, , pic, introduce, tips], (error, result) => {
    if (error) throw error;
    res.send({ message: "ok", code: 200 });
  });
});

/**首页关注 */
// c.get("/focus", (req, res) => {
//   // 获取cid的值
//   pool.query("select * from focus",(err,result)=>{
//     if(err) throw err;
//     res.send(result);
//   });
// });
// 首页关注图片
c.get("/focus/:id", (req, res) => {
  let obj = req.params.id;
  let obj1 = 1 + (obj - 1) * 3;
  let obj2 = 3 * obj;
  let sql =
    " select * from ((select * from f_img left outer join focus on f_img.imgId=focus.id) union (select * from f_img right outer join focus on f_img.imgId=focus.id)) as B where B.img IS NOT NULL and (B.id>=? and B.id<=?);";
  pool.query(sql, [obj1, obj2], (err, result) => {
    if (err) throw err;
    //console.log(result);

    // 保存用户图片
    let arr = [];
    let d1 = [],
      d2 = [],
      d3 = [];

    // 保存用户信息数据，没有用户图片
    let data = [];
    let i1 = {},
      i2 = {},
      i3 = {};

    // 获取用户id并保存
    let arr2 = [];
    for (let key of result) {
      arr2.push(key.id);
    }
    // 数组去重保存id
    let arr3 = [];
    for (let i = 0; i < arr2.length; i++) {
      if (arr3.indexOf(arr2[i]) == -1) {
        arr3.push(arr2[i]);
      }
    }

    // 根据用户id处理数据，图片和用户信息分类处理
    for (let key of result) {
      if (key.id == arr3[0]) {
        d1.push(key.img);
        i1.id = key.id;
        i1.avatar = key.avatar;
        i1.name = key.name;
        i1.title = key.title;
        i1.describew = key.describew;
        i1.praise = key.praise;
        i1.dateissued = key.dateissued;
      } else if (key.id == arr3[1]) {
        d2.push(key.img);
        i2.id = key.id;
        i2.avatar = key.avatar;
        i2.name = key.name;
        i2.title = key.title;
        i2.describew = key.describew;
        i2.praise = key.praise;
        i2.dateissued = key.dateissued;
      } else if (key.id == arr3[2]) {
        d3.push(key.img);
        i3.id = key.id;
        i3.avatar = key.avatar;
        i3.name = key.name;
        i3.title = key.title;
        i3.describew = key.describew;
        i3.praise = key.praise;
        i3.dateissued = key.dateissued;
      }
    }
    // 把处理好图片数据和信息数据分别保存在arr和data数组中
    arr.push(d1, d2, d3);
    data.push(i1, i2, i3);
    //console.log(arr);
    //console.log(data);

    // 最后把图片保存在对应的用户信息数组上
    for (let i = 0; i < data.length; i++) {
      //console.log(data[i]);
      if (data[i].id == arr3[0]) {
        data[i].img = arr[0];
      } else if (data[i].id == arr3[1]) {
        data[i].img = arr[1];
      } else if (data[i].id == arr3[2]) {
        data[i].img = arr[2];
      }
    }
    console.log(data);
    res.send(data);
  });
});
/**首页结束 */

/**用户发布的粥谱 */
c.post("/addcongee", (req, res) => {});

//用户动态查询
c.get("/dynamic/:did", (req, res) => {
  // 获取did的值
});

// 添加收藏
c.get("/collection", (req, res) => {
  
  let obj = req.query;
  console.log(obj);
  let sql =
    "INSERT INTO collection(coll_id,coll_title,coll_pass,coll_pic,coll_score,coll_username) VALUES(null,?,?,?,?,?)";
  pool.query(
    sql,
    [obj.coll_title, obj.coll_pass, obj.coll_pic, 6, obj.coll_username],
    (err, result) => {
      if (err) {
        throw err;
      }
      res.send({ message: "ok", code: 200 });
    }
  );
});
// 收藏查询
c.get("/list/lid", (req, res) => {});

module.exports = c;
